package handler

import (
	"context"
	"encoding/json"
	"errors"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"sale_system/goods_srv/global"
	"sale_system/goods_srv/model"
	"sale_system/goods_srv/proto"
)

// 商品分类接口

func (s *GoodsServer) GetAllCategorysList(ctx context.Context, request *emptypb.Empty) (*proto.CategoryListResponse, error) {

	var categorys []model.Category
	// 直接将三层级打出来
	result := global.DB.Where(&model.Category{Level: 1}).Preload("SubCategory.SubCategory").Find(&categorys) // 直接查这张表，获取所有数据
	if result.RowsAffected == 0 {
		return nil, errors.New("查找不到")
	}
	rsp := &proto.CategoryListResponse{}
	rsp.Total = int32(len(categorys))

	jsonData, _ := json.Marshal(&categorys)

	rsp.JsonData = string(jsonData)

	return rsp, nil
}

// 获取子分类接口

func (s *GoodsServer) GetSubCategory(ctx context.Context, request *proto.CategoryListRequest) (*proto.SubCategoryListResponse, error) {

	var category model.Category
	result := global.DB.First(&category, request.Id)
	if result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "商品类别不存在")
	}

	rsp := proto.SubCategoryListResponse{}

	// 当前这个子分类的信息
	rsp.Info = &proto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           category.Name,
		Level:          category.Level,
		IsTab:          category.IsTab,
		ParentCategory: category.ParentCategoryID,
	}

	// 查看不同等级类目
	var subCategorys []model.Category

	// 这里不用payload，因为只需要打出下一层级出来即可
	global.DB.Where(&model.Category{ParentCategoryID: request.Id}).Find(&subCategorys)

	for _, subCategory := range subCategorys {

		subInfo := &proto.CategoryInfoResponse{
			Id:             subCategory.ID,
			Name:           subCategory.Name,
			Level:          subCategory.Level,
			IsTab:          subCategory.IsTab,
			ParentCategory: subCategory.ParentCategoryID,
		}
		rsp.SubCategorys = append(rsp.SubCategorys, subInfo)
	}
	return &rsp, nil
}

func (s *GoodsServer) CreateCategory(ctx context.Context, request *proto.CategoryInfoRequest) (*proto.CategoryInfoResponse, error) {

	// 这个类目是不是允许重名？？？我们先不做控制
	category := model.Category{}
	category.Name = request.Name
	category.Level = request.Level
	category.IsTab = request.IsTab

	if request.Level != 1 { // 二级类目、三级类目需要设置父类目

		// 一般来说我们是通过web端来选择父类目的，所以不需要判断是否存在
		category.ParentCategoryID = request.ParentCategory
	}
	result := global.DB.Save(&category)
	if result.RowsAffected == 0 {
		return nil, result.Error
	}

	return &proto.CategoryInfoResponse{Id: category.ID, Name: category.Name, Level: category.Level, IsTab: category.IsTab}, nil
}

func (s *GoodsServer) DeleteCategory(ctx context.Context, request *proto.DeleteCategoryRequest) (*emptypb.Empty, error) {

	result := global.DB.First(&model.Category{}, request.Id)
	if result.RowsAffected == 0 {
		return nil, status.Error(codes.NotFound, "商品品类不存在")
	}

	result = global.DB.Delete(&model.Category{}, request.Id)
	if result.RowsAffected == 0 {
		return nil, result.Error
	}

	return &emptypb.Empty{}, nil
}

func (s *GoodsServer) UpdateCategory(ctx context.Context, request *proto.CategoryInfoRequest) (*emptypb.Empty, error) {

	category := model.Category{}

	// 更新前需要控制默认值
	if request.Name != "" {
		category.Name = request.Name
	}

	if request.IsTab == true {
		category.IsTab = request.IsTab
	}

	if request.ParentCategory != 0 {
		category.ParentCategoryID = request.ParentCategory
	}

	if request.Level != 0 {
		category.Level = request.Level
	}

	result := global.DB.Save(&category)
	if result.RowsAffected == 0 {
		return nil, result.Error
	}
	return &emptypb.Empty{}, nil
}
